﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using DAL.BAL;
using DevExpress.Xpf.Editors.Settings;

namespace WpfClient.Windows
{
    /// <summary>
    /// Interaction logic for Backup.xaml
    /// </summary>
    public partial class Backup : Window
    {
        public Backup()
        {
            InitializeComponent();

            gridControl1.View.AllowEditing = false;

            Refresh();
        }

        private void RefreshItem_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
        {
            Refresh();
        }

        private void Refresh()
        {
            try
            {
                Business.Refresh(System.Data.Objects.RefreshMode.ClientWins, Business.MyEntities.Backup);

                gridControl1.ItemsSource = Business.GetBackupHistory();

                ((ComboBoxEditSettings)gridControl1.Columns["BackUpType"].EditSettings).ItemsSource = Business.GetBackupTypes();

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, WpfClient.Properties.Resources.Error, MessageBoxButton.OK);
            }
        }

        private void bDoBackup_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
        {
            DAL.Models.BackUpHistory bh = new DAL.Models.BackUpHistory();

            Business.AddObject("BackUpHistories", bh);

            try
            {
                bh.BackupDate = DateTime.Now;

                bh.BackUpType = (from n in Business.GetBackupTypes()
                                 where n.Id == 2
                                 select n).FirstOrDefault();

                Business.Save();

                bh.Status = Business.sp_Backup();

                Business.Save();

            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message, WpfClient.Properties.Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error);
            }

            Refresh();
        }

        private void bOptions_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
        {
            try
            {
                BackUpOptions bo = new BackUpOptions();

                if (bo.ShowDialog() == false)
                {
                    Business.Cancel();

                    //Business.Refresh(System.Data.Objects.RefreshMode.StoreWins, do);
                }
                else
                    Business.Save();

            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message, WpfClient.Properties.Resources.Error, MessageBoxButton.OK);
            }

        }
    }
}
